package com.firefoxking.mysite.service.site.impl;

import com.firefoxking.mysite.constant.Types;
import com.firefoxking.mysite.dao.AttAchMapper;
import com.firefoxking.mysite.dao.CommentMapper;
import com.firefoxking.mysite.dao.ContentMapper;
import com.firefoxking.mysite.dao.MetadataMapper;
import com.firefoxking.mysite.dao.dto.CommentCond;
import com.firefoxking.mysite.dao.dto.ContentCond;
import com.firefoxking.mysite.dao.dto.StatisticsDto;
import com.firefoxking.mysite.entity.Comment;
import com.firefoxking.mysite.entity.Content;
import com.firefoxking.mysite.service.site.SiteService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SiteServiceImpl implements SiteService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SiteServiceImpl.class);

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private MetadataMapper metadataMapper;

    @Autowired
    private AttAchMapper attAchMapper;

    @Override
    public List<Comment> getComments(int limit) {
        LOGGER.debug("Enter recentComments method:limit={}", limit);
        if (limit < 0 || limit > 10) {
            limit = 10;
        }
        PageHelper.startPage(1, limit);
        List<Comment> rs = commentMapper.getCommentsByCond(new CommentCond());
        LOGGER.debug("Exit recentComments method");
        return rs;
    }

    @Override
    public List<Content> getNewArticles(int limit) {
        LOGGER.debug("Enter recentArticles method:limit={}", limit);
        if (limit < 0 || limit > 10)
            limit = 10;
        PageHelper.startPage(1, limit);
        List<Content> rs = contentMapper.getArticlesByCond(new ContentCond());
        LOGGER.debug("Exit recentArticles method");
        return rs;
    }

    @Override
    public StatisticsDto getStatistics() {
        LOGGER.debug("Enter recentStatistics method");
        //文章总数
        Long artices = contentMapper.getArticleCount();

        Long comments = commentMapper.getCommentsCount();

        Long links = metadataMapper.getMetadataCountByType(Types.LINK.getType());

        Long atts = attAchMapper.getAttsCount();

        StatisticsDto rs = new StatisticsDto();
        rs.setArticles(artices);
        rs.setAttachs(atts);
        rs.setComments(comments);
        rs.setLinks(links);

        LOGGER.debug("Exit recentStatistics method");
        return rs;
    }
}
